package com.yuniarti.konsultasikesehatan;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PasswordActivity extends Activity {

	String id;
	String pass_lama,pass,user;
	EditText pas_lama, pas_baru;
	
	DataParser jsonParser = new DataParser();
	private static String ganti_password = "http://konsultasi-yakes.web.id/JSON/re-password.php";
	//private static String ganti_password = "http://10.0.2.2/adminweb/JSON/re-password.php";
	
	private static final String TAG_HASIL = "hasil";
    private static final String AR_ID = "id_member";
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_password);
		
		Intent i = getIntent();
        id = i.getStringExtra(AR_ID);
        
		pas_lama = (EditText) findViewById(R.id.pass_lama);
        pas_baru = (EditText) findViewById(R.id.pass_baru);
        
        Button reset = (Button) findViewById(R.id.btn_reset_pass);
        reset.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		pas_lama.setText("");
        		pas_baru.setText("");
        	}
        });
        
        Button simpan = (Button) findViewById(R.id.btn_ganti);
        simpan.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		if((pas_lama.length()==0) && (pas_baru.length()==0)){
        			Toast.makeText(PasswordActivity.this, "Anda belum mengisi data",
        					Toast.LENGTH_LONG).show();
        		}else if(pas_lama.length()==0){
        			Toast.makeText(PasswordActivity.this, "Password Lama belum diisi",
        					Toast.LENGTH_LONG).show();
        		}else if(pas_baru.length()==0){
        			Toast.makeText(PasswordActivity.this, "Password Baru belum diisi",
        					Toast.LENGTH_LONG).show();
        		}else{
        		
        		String pass_lama = pas_lama.getText().toString();
        		String pass_baru = pas_baru.getText().toString();

				List<NameValuePair> params = new ArrayList<NameValuePair>();

		            params.add(new BasicNameValuePair("id_member", id));
		            params.add(new BasicNameValuePair("pass_lama", pass_lama));
		            params.add(new BasicNameValuePair("pass_baru", pass_baru));
		 
		        JSONObject json = jsonParser.makeHttpRequest(ganti_password,"POST", params);
		        
		        try {
	            	
	                int hasil = json.getInt(TAG_HASIL);
	 
	                if (hasil == 1) {
	                  
	                	Toast.makeText(PasswordActivity.this, "Ganti Password Berhasil YESSS!", 
	                			Toast.LENGTH_SHORT).show();
	                //	txt_username.setText("Login Berhasil");
	                	
	                    Intent i = new Intent(getApplicationContext(), MenuMemberActivity.class);
	                    i.putExtra(AR_ID, id);
	                    startActivity(i);
	                    finish();
	                    
	                }
	                else {
	                	
	                	Toast.makeText(PasswordActivity.this, "Ganti Password Gagal, " +
	                			"Password lama anda salah", Toast.LENGTH_SHORT).show();
	                //	txt_username.setText("Login Gagal");       
	                }
	            	
	                
	            } catch (JSONException e) {
	                e.printStackTrace();
	            }
        	}}
        });
	}

}
